<template>
  <div v-if="ishead && itemdata.lCanOutput === '1'">
    <template v-if="itemdata.lCanOutput === '0'">
      <input v-model="itemdata.value" style="display: none;">
    </template>
    <template v-else-if="itemdata.lCanInput === '0'">
      <van-field size="large" type="number" readonly clickable :id="itemdata.sField" placeholder="系统自动填充" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardDub = true" />
      <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardDub" @close="showKeyboardDub = false" :show="showKeyboardDub" @blur="showKeyboardDub = false" theme="custom" :extra-key="['00', '.']" close-button-text="完成" />
    </template>
    <template v-else-if="itemdata.lCanInput === '1' && itemdata.lMustInput === '1'">
      <van-field size="large" type="number" readonly clickable required :id="itemdata.sField" placeholder="请输入" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardDub = true" />
      <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardDub" @close="showKeyboardDub = false" :show="showKeyboardDub" @blur="onblur(itemdata)" @input="checkData(itemdata)" theme="custom" :extra-key="['00', '.']" close-button-text="完成" />
    </template>
    <template v-else>
      <van-field size="large" type="number" readonly clickable :id="itemdata.sField" placeholder="请输入" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardDub = true" />
      <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardDub" @close="showKeyboardDub = false" :show="showKeyboardDub" @blur="showKeyboardDub = false" theme="custom" :extra-key="['00', '.']" close-button-text="完成" />
    </template>
  </div>
  <div v-else>
    <template v-if="itemdata.lCanOutput === '0'">
      <input v-model="itemdata.value" style="display: none;">
    </template>
    <template v-else-if="itemdata.lCanInput === '0'">
      <van-field size="large" type="number" readonly clickable :id="index + '_' + itemdata.sField" placeholder="系统自动填充" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardDub = true" />
      <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardDub" @close="showKeyboardDub = false" :show="showKeyboardDub" @blur="showKeyboardDub = false" theme="custom" :extra-key="['00', '.']" close-button-text="完成" />
    </template>
    <template v-else-if="itemdata.lCanInput === '1' && itemdata.lMustInput === '1'">
      <van-field size="large" type="number" readonly clickable required placeholder="请输入" :id="index + '_' + itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardDub = true" />
      <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardDub" @close="showKeyboardDub = false" :show="showKeyboardDub" @blur="onblur(itemdata)" @input="checkData(itemdata)" theme="custom" :extra-key="['00', '.']" close-button-text="完成" />
    </template>
    <template v-else>
      <van-field size="large" type="number" readonly clickable placeholder="请输入" :id="index + '_' + itemdata.sField" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" :value="itemdata.value" @click-input="showKeyboardDub = true" />
      <van-number-keyboard v-model="itemdata.value" :overlay="true" :visible="showKeyboardDub" @close="showKeyboardDub = false" :show="showKeyboardDub" @blur="showKeyboardDub = false" theme="custom" :extra-key="['00', '.']" close-button-text="完成" />
    </template>
  </div>
</template>
<script>
import { Toast } from 'vant'
import Vue from 'vue'
import { eventMixins } from '@/mixins/event.js'
Vue.use(Toast)
export default {
  props: {
    itemdata: {
      type: Object,
      required: false
    },
    ishead: {
      type: Boolean,
      default: false
    },
    index: {
      type: Number,
      required: false
    }
  },
  data () {
    return {
      showKeyboardDub: false,
      value: ''
    }
  },
  mounted(){
    console.log(3333)
  },
  watch: {
    'itemdata.value': {
      handler (newval) {
        //console.log(newval)
        this.$emit('handleFiledLink', { sField: this.itemdata.sField, value: newval, ishead: this.ishead, index: this.index })
      
        console.log(this.itemdata)
        // if(itemdata?.event && )
      }
    }
  },
  methods: {
    onblur (datamodel) {
      this.showKeyboardDub = false
      this.checkData(datamodel)
    },
    checkData (datamodel) {
      //console.log('checkData')
      let projectData = require('@/project.json')
      if (projectData.project && projectData.project == 'dinuo/') {
        //console.log('datamodel', datamodel)
        if (datamodel && datamodel.sField == 'quantity') {
          let value = datamodel.value
          if (value && value.indexOf('.') != -1) {
            datamodel.value = value.substring(0, value.indexOf('.'))
            Toast('数量不能有小数')
            return
          }
        }
        if (datamodel && datamodel.sField == 'price2') {
          let value = datamodel.value

          if (value && value.indexOf('.') != -1) {
            let v = value.substring(value.indexOf('.'))
            if (v.length > 3) {
              datamodel.value = value.substring(0, value.indexOf('.') + 3)
              Toast('小数点后面只能有两位')
              return
            }
          }

          if (value && value.startsWith('-')) {
            Toast('结算价不能是负数')
            return
          }
          for (let j = 0; j < this.jsondata.bodydata[this.indexName].length; j++) {
            for (let k = 0; k < this.jsondata.bodydata[this.indexName][j].length; k++) {
              //console.log('this.jsondata.bodydata[this.indexName][j]', this.jsondata.bodydata[this.indexName][j])
              //console.log('this.jsondata.bodydata[this.indexName][j][k]', this.jsondata.bodydata[this.indexName][j][k])

              if (j == this.index) {
                let item = this.jsondata.bodydata[this.indexName][j][k]
                //console.log('item-->', item)
                if (item.sField === 'type') {
                  let typeValue = item.value
                  if (!typeValue) {
                    datamodel.value = ''
                    datamodel.valuehidden = ''
                    Toast('请先选择报量类型')
                    return
                  }
                  if (typeValue == '赠品' || typeValue == '礼品及物料') {
                    datamodel.value = 0
                    datamodel.valuehidden = 0
                    break
                  } else {
                    // 药品
                    let price2Value = datamodel.value
                    if (price2Value && (price2Value == '0' || price2Value == '0.0' || price2Value == '0.00')) {
                      datamodel.value = ''
                      datamodel.valuehidden = ''
                      return
                    }
                  }
                }
              }
            }
          }
        }
      }

      if (datamodel.value === '') {
        datamodel.errormsg = '不能为空'
      } else {
        datamodel.errormsg = ''
      }
    }
  }
}
</script>
